package com.zzyl.nursing.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.nursing.mapper.NursingProjectPlanMapper;
import com.zzyl.nursing.vo.NursingPlanVo;
import com.zzyl.nursing.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 护理计划Service业务层处理
 * 
 * @author guochao
 * @date 2025-08-31
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id)
    {
        //根据id查询护理数据
        NursingPlan plan=getById(id);
        NursingPlanVo planVo=new NursingPlanVo();
        BeanUtils.copyProperties(plan, planVo);
        //根据id查询护理项目计划数据
        LambdaQueryWrapper<NursingProjectPlan> qw = new LambdaQueryWrapper<>();
        qw.eq(NursingProjectPlan::getPlanId,id);
        List<NursingProjectPlan> projectPlans = nursingProjectPlanMapper.selectList(qw);
        List<NursingProjectPlanVo> voList = projectPlans.stream().map(nursingProjectPlan -> {
            NursingProjectPlanVo vo = new NursingProjectPlanVo();
            BeanUtils.copyProperties(nursingProjectPlan, vo);
            vo.setProjectId(nursingProjectPlan.getProjectId().toString());
            vo.setExecuteCycle(nursingProjectPlan.getExecuteCycle().toString());
            return vo;
        }).collect(Collectors.toList());
        planVo.setProjectPlans(voList);
        return planVo;
    }

    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     * 
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int insertNursingPlan(NursingPlanDto dto)
    {
       //新增护理计划(使用工具栏实现dto转换实体)
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyBeanProp(nursingPlan,dto);
        int num = this.nursingPlanMapper.insertNursingPlan(nursingPlan);

        //新增护理计划和护理项目的关联关系数据
        List<NursingProjectPlan> projectPlans = dto.getProjectPlans();
        for (NursingProjectPlan projectPlan : projectPlans) {
            projectPlan.setPlanId(nursingPlan.getId());
            nursingProjectPlanMapper.insert(projectPlan);
        }
        return num;
    }

    /**
     * 修改护理计划
     * 
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int updateNursingPlan(NursingPlanDto dto)
    {
        //1.修改护理计划
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyBeanProp(nursingPlan, dto);
        int num = nursingPlanMapper.updateById(nursingPlan);
        //2.修改护理计划对应的护理项目【先删除，再新增】
        List<NursingProjectPlan> projectPlans = dto.getProjectPlans();
        if (!CollectionUtils.isEmpty(projectPlans)){
            //根据检查计划id，删除该检查计划下的所有检查项目
            LambdaQueryWrapper<NursingProjectPlan> qw = new LambdaQueryWrapper<>();
            qw.eq(NursingProjectPlan::getPlanId, dto.getId());
            this.nursingProjectPlanMapper.delete(qw);
            //批量插入
            for (NursingProjectPlan projectPlan : projectPlans) {
                projectPlan.setPlanId(dto.getId());
                this.nursingProjectPlanMapper.insert(projectPlan);
            }
        }
        return num;

    }

    /**
     * 批量删除护理计划
     * 
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids)
    {
        //删除护理计划
        int num = this.nursingPlanMapper.deleteNursingPlanByIds(ids);
        //删除护理计划项目
        LambdaQueryWrapper<NursingProjectPlan> qw = new LambdaQueryWrapper<>();
        qw.in(NursingProjectPlan::getPlanId, ids);
        this.nursingProjectPlanMapper.delete(qw);
        return num;
    }

    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id)
    {
        //删除护理计划
        int num = this.nursingPlanMapper.deleteNursingPlanById(id);
        //删除护理计划项目
        LambdaQueryWrapper<NursingProjectPlan> qw = new LambdaQueryWrapper<>();
        qw.in(NursingProjectPlan::getPlanId, id);
        this.nursingProjectPlanMapper.delete(qw);
        return num;
    }
}
